package ui.fragment.company;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.company.AllCompanyFeedAdapter;
import controller.company.AllCompanyFeedAsync;
import model.AllCompanyFeedItem;
import model.company.CompanyFeedItem;
import ui.activity.login.Login;

public class AllCompanyFeedFragment extends Fragment
        implements View.OnClickListener{

    /**
     * String section
     */
    public static Boolean IS_IN_ALL_COMPANY_FEED_FRAGMENT = false;
    /**
     * View section
     */

    public static ListView mLvInAllCompanyFeedFragment;
    public static AllCompanyFeedAdapter allCompanyFeedAdapter = null;
    /**
     * Others section
     */

    /**
     * Listener section
     */

    public static Fragment newInstance() {

        AllCompanyFeedFragment fragment = new AllCompanyFeedFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_all_company_feed, container, false);
        IS_IN_ALL_COMPANY_FEED_FRAGMENT = true;
        // Initial views
        initialViews(v);
        initialData();
        //set data for company feed fragment
        setDataForCompanyFeedFragment ();
        return v;
    }

    @Override
    public void onStop() {
        super.onStop();
        IS_IN_ALL_COMPANY_FEED_FRAGMENT = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        IS_IN_ALL_COMPANY_FEED_FRAGMENT = false;
        //TODO will remove after finish all ibtn on actionbar
        //TODO mean write function invisible all the others ibtn and only visible ibtn at current page
        //invisible ibtn create group
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.VISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }
    /**
     * Initialize methods
     */
    private void initialData() {
        allCompanyFeedAdapter = new AllCompanyFeedAdapter(
                getActivity(), R.layout.simple_list_item_all_company_feed, AllCompanyFeedAsync.mAlAllCompanyFeed);
        mLvInAllCompanyFeedFragment.setAdapter(allCompanyFeedAdapter);
        //visible all ibtns
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.INVISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }

    private void initialViews(View v) {
        mLvInAllCompanyFeedFragment = (ListView) v.findViewById(R.id.lv_in_all_company_feed_fragment);
    }

    private void setDataForCompanyFeedFragment () {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new AllCompanyFeedAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()), String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    getActivity().getString(R.string.feed), "0");
        else
            new AllCompanyFeedAsync(getActivity()).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()), String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    getActivity().getString(R.string.feed), "0");
    }
}
